#!/bin/bash

echo "===================================="
echo "    MMC 后台管理系统启动脚本"
echo "===================================="
echo

# 检查是否安装了必要的工具
command -v java >/dev/null 2>&1 || { echo "错误: 需要安装 Java 17+"; exit 1; }
command -v mvn >/dev/null 2>&1 || { echo "错误: 需要安装 Maven"; exit 1; }
command -v node >/dev/null 2>&1 || { echo "错误: 需要安装 Node.js 16+"; exit 1; }
command -v npm >/dev/null 2>&1 || { echo "错误: 需要安装 npm"; exit 1; }

echo "正在启动后端服务..."
cd backend
nohup mvn spring-boot:run > ../backend.log 2>&1 &
BACKEND_PID=$!
cd ..

echo "等待后端服务启动..."
sleep 10

echo "正在安装前端依赖..."
cd frontend
if [ ! -d "node_modules" ]; then
    npm install
fi

echo "正在启动前端服务..."
nohup npm run dev > ../frontend.log 2>&1 &
FRONTEND_PID=$!
cd ..

echo
echo "启动完成！"
echo "前端地址: http://localhost:3000"
echo "后端地址: http://localhost:8080"  
echo "API文档: http://localhost:8080/api/doc.html"
echo
echo "默认账号：admin"
echo "默认密码：admin123"
echo
echo "后端进程 PID: $BACKEND_PID"
echo "前端进程 PID: $FRONTEND_PID"
echo
echo "要停止服务，请运行: kill $BACKEND_PID $FRONTEND_PID"
echo "或者运行: ./stop.sh"

# 保存PID到文件
echo $BACKEND_PID > backend.pid
echo $FRONTEND_PID > frontend.pid 